﻿
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Business.Base.DB.Query;
using Business.Base.Enums;
using Business.Base.Model;
using Business.Base.Service.Create;
using Business.CentrifugalPump.Perf;
using Pumptester.ExperimentForms.Base;
using Pumptester.ExperimentList;

namespace Pumptester
{
    public partial class ExperimentListForm : Form
    {
        Dictionary<ExperimentType, ExperimentListQueryAction> experimentDic;
        ExperimentListQueryAction[] ListQueryAction;
        String txtLable = "当前查询到{0}条记录，共{1}页";



        /// <summary>
        /// 加载历史数据
        /// </summary>
        /// <param name="type"></param>
        private void loadData(ExperimentType type)
        {

        }

        private void queryData()
        {

        }

        private void loadCentrifugalPumpPerfDataGridView(DataGridView dataGridView, List<object> list)
        {
            dataGridView.Rows.Clear();
            Type type = null;
            if (list.Count != 0)
            {
                type = list[0].GetType();
            }
            else
            {
                return ;
            }
            for (int i = 0; i < list.Count; i++)
            {
                object experiment = (object)list[i];
                DataGridViewRow row = (DataGridViewRow)dataGridView.RowTemplate.Clone();


                

                DataGridViewTextBoxCell codeCell = new DataGridViewTextBoxCell();
                codeCell.Value = type.GetProperty("ExperimentCode").GetValue(experiment);

                DataGridViewTextBoxCell nameCell = new DataGridViewTextBoxCell();
                nameCell.Value = type.GetProperty("PumpName").GetValue(experiment);

                DataGridViewTextBoxCell customCell = new DataGridViewTextBoxCell();
                customCell.Value = type.GetProperty("Customer").GetValue(experiment);

                DataGridViewTextBoxCell pumpModelCell = new DataGridViewTextBoxCell();
                pumpModelCell.Value = type.GetProperty("PumpModel").GetValue(experiment);

                DataGridViewTextBoxCell pumpCodeCell = new DataGridViewTextBoxCell();
                pumpCodeCell.Value = type.GetProperty("PumpCode").GetValue(experiment);

                DataGridViewTextBoxCell manufacturerCell = new DataGridViewTextBoxCell();
                manufacturerCell.Value = type.GetProperty("Manufacturer").GetValue(experiment);

                DataGridViewTextBoxCell experimentOwnerCell = new DataGridViewTextBoxCell();
                experimentOwnerCell.Value = type.GetProperty("ExperimentOwner").GetValue(experiment);

                DataGridViewTextBoxCell createTimeCell = new DataGridViewTextBoxCell();
                createTimeCell.Value = type.GetProperty("CreateTime").GetValue(experiment);


                row.Cells.Add(codeCell);
                row.Cells.Add(nameCell);
                row.Cells.Add(customCell);
                row.Cells.Add(pumpModelCell);
                row.Cells.Add(pumpCodeCell);
                row.Cells.Add(manufacturerCell);
                row.Cells.Add(experimentOwnerCell);
                row.Cells.Add(createTimeCell);
                row.Tag = experiment;
                dataGridView.Rows.Add(row);
            }
        }
        /*
        private void loadCentrifugalPumpPerfDataGridView(DataGridView dataGridView,List<object> list)
        {
            dataGridView.Rows.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                CentrifugalPumpPerfExperiment experiment = (CentrifugalPumpPerfExperiment)list[i];
                DataGridViewRow row = (DataGridViewRow)dataGridView.RowTemplate.Clone();

                DataGridViewTextBoxCell codeCell = new DataGridViewTextBoxCell();
                codeCell.Value = experiment.ExperimentCode;

                DataGridViewTextBoxCell nameCell =     new DataGridViewTextBoxCell();
                nameCell.Value = experiment.PumpName ;

                DataGridViewTextBoxCell customCell =   new DataGridViewTextBoxCell();
                customCell.Value = experiment.Customer;

                DataGridViewTextBoxCell pumpModelCell = new DataGridViewTextBoxCell();
                pumpModelCell.Value = experiment.PumpModel;

                DataGridViewTextBoxCell pumpCodeCell = new DataGridViewTextBoxCell();
                pumpCodeCell.Value = experiment.PumpCode;

                DataGridViewTextBoxCell manufacturerCell= new DataGridViewTextBoxCell();
                manufacturerCell.Value = experiment.Manufacturer;

                DataGridViewTextBoxCell experimentOwnerCell = new DataGridViewTextBoxCell();
                experimentOwnerCell.Value = experiment.ExperimentOwner;

                DataGridViewTextBoxCell createTimeCell = new DataGridViewTextBoxCell();
                createTimeCell.Value = experiment.CreateTime;


                row.Cells.Add(codeCell);
                row.Cells.Add(nameCell);
                row.Cells.Add(customCell);
                row.Cells.Add(pumpModelCell);
                row.Cells.Add(pumpCodeCell);
                row.Cells.Add(manufacturerCell);
                row.Cells.Add(experimentOwnerCell);
                row.Cells.Add(createTimeCell);
                row.Tag = experiment;
                dataGridView.Rows.Add(row);
            }
            

           
        }
        */
        /// <summary>
        /// 获取潜水泵的查询条件
        /// </summary>
        /// <returns></returns>
        private ExperimentQuery getCentrifugalPumpPerfQuery()
        {
            ExperimentQuery experimentQuery = new ExperimentQuery();
            experimentQuery.Code = this.textBoxCode.Text.Trim();
            experimentQuery.PumpModel = this.textPumpModel.Text.Trim();
            experimentQuery.ExperimentOwner = this.txtBoxTester.Text.Trim();
            experimentQuery.PumpName = this.textBoxPumpName.Text.Trim();

            experimentQuery.Manufacturer = this.textBoxManufacturer.Text;
            experimentQuery.PumpCode = this.textBoxPumpCode.Text;
            experimentQuery.CreateStartTime = this.dateTimePickerStart.Value;
            experimentQuery.CreateEndTime = this.dateTimePickerEnd.Value;
            experimentQuery.Customer = this.textBoxCustomer.Text;
            experimentQuery.PageNum = -1;
            experimentQuery.PageSize = ExperimentListQueryAction.PageSize;
            experimentQuery.OrderBy = "Id";
            experimentQuery.OrderType = "desc";
            return experimentQuery;
        }

        /// <summary>
        /// 获取螺杆泵的查询条件
        /// </summary>
        /// <returns></returns>
        private ExperimentQuery getSPPerfQuery()
        {
            ExperimentQuery experimentQuery = new ExperimentQuery();
            experimentQuery.Code = this.textBoxCodeSP.Text.Trim();
            experimentQuery.PumpModel = this.textPumpModelSP.Text.Trim();
            experimentQuery.ExperimentOwner = this.txtBoxTesterSP.Text.Trim();
            experimentQuery.PumpName = this.textBoxPumpNameSP.Text.Trim();

            experimentQuery.Manufacturer = this.textBoxManufacturerSP.Text;
            experimentQuery.PumpCode = this.textBoxPumpCodeSP.Text;
            experimentQuery.CreateStartTime = this.dateTimePickerStartSP.Value;
            experimentQuery.CreateEndTime   = this.dateTimePickerEndSP.Value;
            experimentQuery.Customer  = this.textBoxCustomerSP.Text;
            experimentQuery.PageNum   = -1;
            experimentQuery.PageSize  = ExperimentListQueryAction.PageSize;
            experimentQuery.OrderBy   = "Id";
            experimentQuery.OrderType = "desc";
            return experimentQuery;
        }

        /// <summary>
        /// 获取汽蚀测试记录的查询条件
        /// </summary>
        /// <returns></returns>
        private ExperimentQuery getNPSHQuery()
        {
            ExperimentQuery experimentQuery = new ExperimentQuery();
            experimentQuery.Code = this.textBoxCodeNPSH.Text.Trim();
            experimentQuery.PumpModel = this.textPumpModelNPSH.Text.Trim();
            experimentQuery.ExperimentOwner = this.txtBoxTesterNPSH.Text.Trim();
            experimentQuery.PumpName = this.textBoxPumpNameNPSH.Text.Trim();

            experimentQuery.Manufacturer = this.textBoxManufacturerNPSH.Text;
            experimentQuery.PumpCode = this.textBoxPumpCodeNPSH.Text;
            experimentQuery.CreateStartTime = this.dateTimePickerStartNPSH.Value;
            experimentQuery.CreateEndTime = this.dateTimePickerEndNPSH.Value;
            experimentQuery.Customer = this.textBoxCustomerNPSH.Text;
            experimentQuery.PageNum = -1;
            experimentQuery.PageSize = ExperimentListQueryAction.PageSize;
            experimentQuery.OrderBy = "Id";
            experimentQuery.OrderType = "desc";
            return experimentQuery;
        }

        /// <summary>
        /// 默认初始化潜水泵查询的开始生产和结束生产的条件。
        /// </summary>
        private void initDataSpanCP()
        {
            this.dateTimePickerEnd.Value = DateTime.Today;
            this.dateTimePickerStart.Value = DateTime.Today.AddDays(-15);
        }

        /// <summary>
        /// 默认初始化螺杆泵查询的开始生产和结束生产的条件。
        /// </summary>
        private void initDataSpanSP()
        {
            this.dateTimePickerEndSP.Value = DateTime.Today;
            this.dateTimePickerStartSP.Value = DateTime.Today.AddDays(-15);
        }

        /// <summary>
        /// 初始化汽蚀余量查询的开始生产和结束生产的条件
        /// </summary>
        private void initDataSpanNPSH()
        {
            this.dateTimePickerEndNPSH.Value = DateTime.Today;
            this.dateTimePickerStartNPSH.Value = DateTime.Today.AddDays(-15);
        }
        

        public ExperimentListForm()
        {
            InitializeComponent();
            initDataSpanCP();
            initDataSpanSP();

            experimentDic = new Dictionary<ExperimentType, ExperimentListQueryAction>();
            ServiceContainer serviceContainer =ServiceContainer.getInstance();

            ExperimentListQueryAction centrifugalPumpPerfExperimentListQueryAction = new ExperimentListQueryAction(serviceContainer[ExperimentType.CentrifugalPumpPerf], this.centrifugalPumpPerfDataGridView);
            experimentDic.Add(ExperimentType.CentrifugalPumpPerf, centrifugalPumpPerfExperimentListQueryAction);
            centrifugalPumpPerfExperimentListQueryAction.loadDataGrid = new LoadDataGrid(loadCentrifugalPumpPerfDataGridView);

            ExperimentListQueryAction spPerfExperimentListQueryAction = new ExperimentListQueryAction(serviceContainer[ExperimentType.ScrewPumpPerf], this.dataGridViewSP);
            experimentDic.Add(ExperimentType.ScrewPumpPerf, spPerfExperimentListQueryAction);
            spPerfExperimentListQueryAction.loadDataGrid = new LoadDataGrid(loadCentrifugalPumpPerfDataGridView);

            ExperimentListQueryAction npshExperimentListQueryAction = new ExperimentListQueryAction(serviceContainer[ExperimentType.NPSH], this.dataGridViewNPSH);
            experimentDic.Add(ExperimentType.NPSH, npshExperimentListQueryAction);
            npshExperimentListQueryAction.loadDataGrid = new LoadDataGrid(loadCentrifugalPumpPerfDataGridView);

        }

        private void ShowView(DataGridView dv, DataGridViewCellEventArgs e, ExperimentType experimentType)
        {
            BaseViewForm form = null;

            Experiment experiment = (Experiment)dv.Rows[e.RowIndex].Tag;
            experiment.ExperimentType = experimentType;
            List<object> list = experimentDic[experimentType].LoadRecord(experiment.ExperimentCode);
            experiment.RecordList = new List<ExperimentRecord>();
            for (int i = 0; i < list.Count; i++)
            {
                experiment.RecordList.Add((ExperimentRecord)list[i]);
            }
            BaseChartData chart = experimentDic[experimentType].loadChart(experiment);

            switch (experimentType)
            {
                case ExperimentType.CentrifugalPumpPerf: form = new Pumptester.ExperimentForms.CentrifugalPump.Perf.CentrifugalPumpPerfViewForm(experiment, chart, this); break;
                case ExperimentType.ScrewPumpPerf: form = new Pumptester.ExperimentForms.ScrewPump.Perf.ViewForm(experiment, chart, this); break;
                case ExperimentType.NPSH: form = new Pumptester.ExperimentForms.Npsh.ViewForm(experiment, chart, this); break;

            }
            if (form != null)
            {
                this.Hide();
                form.ShowDialog();
            }


        }

        private void ExperimentListForm_Load(object sender, EventArgs e)
        {
            experimentDic[ExperimentType.CentrifugalPumpPerf].Query = getCentrifugalPumpPerfQuery();
            experimentDic[ExperimentType.CentrifugalPumpPerf].LoadData();
            initDataSpanCP();

            experimentDic[ExperimentType.ScrewPumpPerf].Query = getSPPerfQuery();
            experimentDic[ExperimentType.ScrewPumpPerf].LoadData();
            initDataSpanSP();

            experimentDic[ExperimentType.NPSH].Query = getNPSHQuery();
            experimentDic[ExperimentType.NPSH].LoadData();
            initDataSpanNPSH();
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.dateTimePickerStart.Value.AddMonths(1) < this.dateTimePickerEnd.Value)
            {
                MessageBox.Show("搜索的时间段必须在一个月之内!");
                return;
            }
            experimentDic[ExperimentType.CentrifugalPumpPerf].Query = getCentrifugalPumpPerfQuery();
            experimentDic[ExperimentType.CentrifugalPumpPerf].LoadData();
        }

      

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            /*Experiment experiment = (Experiment)this.centrifugalPumpPerfDataGridView.Rows[e.RowIndex].Tag;
            List<object> list = experimentDic[ExperimentType.CentrifugalPumpPerf].LoadRecord(experiment.ExperimentCode);
            
            experiment.RecordList = new List<ExperimentRecord>();
            for (int i = 0; i < list.Count; i++)
            {
                experiment.RecordList.Add((CentrifugalPumpPerfExperimentRecord)list[i]);
            }
            BaseChartData chart = experimentDic[ExperimentType.CentrifugalPumpPerf].loadChart(experiment);
            BaseViewForm form;

            form = new Pumptester.ExperimentForms.CentrifugalPump.Perf.CentrifugalPumpPerfViewForm(experiment,chart,this);
            this.Hide();            
            form.ShowDialog();   */

            ShowView((DataGridView)sender,e, ExperimentType.CentrifugalPumpPerf);

        }

        private void dataGridViewSP_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ShowView((DataGridView)sender,e, ExperimentType.ScrewPumpPerf);
        }

        private void buttonClearCondition_Click(object sender, EventArgs e)
        {
            this.textBoxCode.Text = "";
            this.textPumpModel.Text = "";
            this.txtBoxTester.Text = "";
            this.textBoxPumpName.Text = "";
            this.textBoxManufacturer.Text = "";
            this.textBoxPumpCode.Text = "";
            this.textBoxCustomer.Text = "";
            this.initDataSpanCP();
            
        }

        private void btnClearSp_Click(object sender, EventArgs e)
        {
            this.textBoxCodeSP.Text = "";
            this.textPumpModelSP.Text = "";
            this.txtBoxTesterSP.Text = "";
            this.textBoxPumpNameSP.Text = "";
            this.textBoxManufacturerSP.Text = "";
            this.textBoxPumpCodeSP.Text = "";
            this.textBoxCustomerSP.Text = "";
            this.initDataSpanSP();
        }

        private void btnSearchSp_Click(object sender, EventArgs e)
        {
            if (this.dateTimePickerStartSP.Value.AddMonths(1) < this.dateTimePickerEndSP.Value)
            {
                MessageBox.Show("搜索的时间段必须在一个月之内!");
                return;
            }
            experimentDic[ExperimentType.ScrewPumpPerf].Query = this.getSPPerfQuery();
            experimentDic[ExperimentType.ScrewPumpPerf].LoadData();
        }

        private void btnSearchNpsh_Click(object sender, EventArgs e)
        {
            if (this.dateTimePickerStartNPSH.Value.AddMonths(1) < this.dateTimePickerEndNPSH.Value)
            {
                MessageBox.Show("搜索的时间段必须在一个月之内!");
                return;
            }
            experimentDic[ExperimentType.NPSH].Query = this.getNPSHQuery();
            experimentDic[ExperimentType.NPSH].LoadData();
        }

        private void btnClearNPSH_Click(object sender, EventArgs e)
        {
            this.textBoxCodeNPSH.Text = "";
            this.textPumpModelNPSH.Text = "";
            this.txtBoxTesterNPSH.Text = "";
            this.textBoxPumpNameNPSH.Text = "";
            this.textBoxManufacturerNPSH.Text = "";
            this.textBoxPumpCodeNPSH.Text = "";
            this.textBoxCustomerNPSH.Text = "";
            this.initDataSpanNPSH();
        }

        private void dataGridViewNPSH_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ShowView((DataGridView)sender, e, ExperimentType.NPSH);
        }
    }
}
